package com.all.play.mypsm.home.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.all.play.mypsm.R;
import com.all.play.mypsm.base.BaseFragment;
import com.all.play.mypsm.home.adapter.HomeFragmentAdapter;
import com.all.play.mypsm.home.bean.HomeResaultBeanData;
import com.all.play.mypsm.utils.Constants;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * Created by 聪明一只哈 on 2018/3/31.
 */

public class HomeFragment extends BaseFragment {


    private static final String TAG = HomeFragment.class.getSimpleName();

    private RecyclerView rvHome;
    private ImageView ib_top;
    private TextView tv_search_home;
    private TextView tv_message_home;
    private HomeResaultBeanData.ResultBean resultBean;
    private HomeFragmentAdapter adapter;

    @Override

    public View initView() {
        Log.e(TAG, "主页视图被初始化了");
        View view = View.inflate(mContext, R.layout.fragment_home, null);
        rvHome = (RecyclerView) view.findViewById(R.id.rv_home);
        ib_top = (ImageView) view.findViewById(R.id.ib_top);
        tv_search_home = (TextView) view.findViewById(R.id.tv_search_home);
        tv_message_home = (TextView) view.findViewById(R.id.tv_message_home);
        //设置点击事件
        initListener();
        return view;
    }

    @Override

    public void initData() {
        super.initData();
        Log.e("TAG", "主页数据被初始化了");

        getNetData();

    }

    /**
     * 获取网络数据
     *
     * @author 聪明一只哈
     * @time 2018/3/31 10:36
     */
    private void getNetData() {
        String url = Constants.HOME_URL;
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("getNetData", "onResponse: " + response);
                        //解析数据
                        precessData(response);

                    }
                });
    }

    /**
     * 解析数据
     *
     * @author 聪明一只哈
     * @time 2018/3/31 10:56
     */
    private void precessData(String response) {
        HomeResaultBeanData homeResaultBeanData = JSON.parseObject(response, HomeResaultBeanData.class);
        if (homeResaultBeanData != null) {
            resultBean = homeResaultBeanData.getResult();
            if (resultBean != null) {
                //有数据
                adapter = new HomeFragmentAdapter(mContext,resultBean);
                rvHome.setAdapter(adapter);

                //利用布局管理器,来监听滑动,以此来决定显示还是隐藏悬浮按钮
                GridLayoutManager layoutManager = new GridLayoutManager(mContext, 1);
                //设置跨度大小的监听
                layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {

                        if(position<=3){
                            ib_top.setVisibility(View.GONE);
                        }else{
                            ib_top.setVisibility(View.VISIBLE);
                        }
                        return 1;//默认是0,逻辑中只能设为1
                    }
                });

                //rv设置布局管理者
                rvHome.setLayoutManager(layoutManager);

            } else {
                //没有数据


            }
        }

    }

    private void initListener() {
        //置顶的监听
        ib_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //回到顶部
                rvHome.scrollToPosition(0);
            }
        });
        //搜素的监听
        tv_search_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "搜索", Toast.LENGTH_SHORT).show();

            }

        });

        //消息的监听
        tv_message_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "进入消息中心", Toast.LENGTH_SHORT).show();

            }

        });


    }
}
